#!/bin/sh
#
#
#
#
#
#
# command line processing code lifted from http://bashcurescancer.com/media/scripts/monitorCpuUsage.sh.txt 

usageHelp="Usage: ${0##*/}"
filenameHelp="-f file, a path to the file containing your notes"
userHelp="-u userid, your delicious.com user"
passHelp="-p password, your delicious.com password"
replaceHelp="-r 0 or 1 to denote whether you want to replace postings. Defaults to false (NOT IMPLEMENTED)"
badOptionHelp="Option not recognised"
printHelpAndExit()
{
	echo "$usageHelp"
	echo "$filenameHelp"
	echo "$userHelp"
	echo "$passHelp"
	echo "$replaceHelp"
	exit $1
}
printErrorHelpAndExit()
{
        echo
        echo "$@"
        echo
        echo
        printHelpAndExit 1
}

# copied from http://wiki.linuxquestions.org/wiki/Bash_tips 
removedotdot()
{
while true; do
        case "$filename" in
                ( /* )
                # Remove /./ inside filename:
                while echo "$filename" |fgrep "/./" >/dev/null 2>&1; do
                        filename=`echo "$filename" | sed "s/\\/\\.\\//\\//"`
                done
                # Remove /../ inside filename:
                while echo "$filename" |grep "/[^/][^/]*/\\.\\./" >/dev/null 2>&1; do
                        filename=`echo "$filename" | sed "s/\\/[^/][^/]*\\/\\.\\.\\//\\//"`
                done
                echo "==>$filename"
                break; 
                ;;

                (*)
                filename=`pwd`/"$filename"
                ;;
        esac
        echo "------"
done
}

filename=""
user=""
pass=""
replace="false"

while getopts "hf:u:p:r:" optname; do
case "$optname" in
h) printHelp 0;;
f) filename="$OPTARG";;
u) user="$OPTARG";;
p) pass="$OPTARG";;
[?]) printErrorHelpAndExit "$badOptionHelp";;
esac
done

[[ -z "$filename" ]] && printErrorHelpAndExit "$filenameHelp"
[[ -z "$user" ]] && printErrorHelpAndExit "$userHelp"
[[ -z "$pass" ]] && printErrorHelpAndExit "$passHelp"


echo "original $filename"
removedotdot
echo "modified $filename"

cd target
java -jar corynotes-client-1.0.0.jar "$filename" "$user" "$pass" 
